<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Help Desk
 * @version 	1.0
 * @author 		Kurosaki <kurosaki@zprofess.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

class ZstatusObject extends ObjectModel
{
	public 		$id;


	/** @var string Name */
	public 		$status;

	/** @var boolean Status for display */
	public 		$active = 1;


	/** @var string Description */
	public 		$description;

	/** @var string Object creation date */
	public 		$date_add;

	/** @var string Backgroud color for status */
	public 		$backgroud_color='#ffffff';
	
	/** @var string text color for status */
	public 		$text_color='#000000';
	/** @var int number position for status */
	public 		$position;
	
	public 		$id_zhelpdesk_ticket_status;
	 
	
	protected $tables = array ('zhelpdesk_ticket_status', 'zhelpdesk_ticket_status_lang');

	protected 	$fieldsRequired = array('active');
 	protected 	$fieldsSize = array('active' => 1);
 	protected 	$fieldsValidate = array('active' => 'isBool');
	protected 	$fieldsRequiredLang = array('status');
 	protected 	$fieldsSizeLang = array('status' => 64);
 	protected 	$fieldsValidateLang = array('status' => 'isCatalogName','description' => 'isCleanHtml');

	protected 	$table = 'zhelpdesk_ticket_status';
	protected 	$identifier = 'id_zhelpdesk_ticket_status';

	public function __construct($id = NULL, $id_lang = NULL) {
		parent::__construct($id, $id_lang);
	}

	public function getFields()
	{
		parent::validateFields();
		if (isset($this->id))
			$fields['id_zhelpdesk_ticket_status'] = (int)($this->id);
		$fields['active'] = (int)($this->active);
		$fields['date_add'] = pSQL($this->date_add);
		$fields['backgroud_color'] = pSQL($this->backgroud_color);
		$fields['text_color'] = pSQL($this->text_color);
		$fields['position'] = pSQL($this->position);
		
		return $fields;
	}

	/**
	  * Check then return multilingual fields for database interaction
	  *
	  * @return array Multilingual fields
	  */
	public function getTranslationsFieldsChild()
	{
		parent::validateFieldsLang();
		return parent::getTranslationsFields(array('status', 'description'));
	}

	
	/**
	 * @see ObjectModel::toggleStatus()
	 */
	public function toggleStatus()
	{
		$result = parent::toggleStatus();
		return $result;
	}

	/** this function return the number of category + 1 having $id_zhelpdesk_ticket_status_parent as parent.
	 * 
	 * @todo rename that function to make it understandable (getNewLastPosition for example)
	 * @param int $id_zhelpdesk_ticket_status_parent the parent category
	 * @return int
	 */
	public static function getLastPosition()
	{
		return (Db::getInstance()->getValue('SELECT MAX(position)+1 FROM `' . _DB_PREFIX_ .'zhelpdesk_ticket_status`'));
	}
    
    
	/**
	 * display color for list content width item backround-color and text-color
	 * @param unknown_type $list
	 * 					list item display bf
	 */
	public static function getStyleListContent ($list){
		$nb = self::getLastPosition();
		$rs= '
		<style type="text/css"> ';
		foreach ($list as $item) {
			for ($i=0;$i<$nb;$i++) 
				$rs.='
					tr#tr_1_'.$item['id_zhelpdesk_ticket_status'].'_'.$i.' td:nth-child(3) {
						background-color:'.$item['backgroud_color'].';
						color:'.$item['text_color'].';
						text-align:center;
						font-weight: bold;
						border-radius: 6px;
					}';
		}
		$rs.='	
			</style>';
		return $rs;
	}
	/**
	 * when customer post ticket, we get ticket status default is min
	 */
	public static function getDefaultStatus () {
		return (Db::getInstance()->getValue('SELECT id_zhelpdesk_ticket_rating FROM '. _DB_PREFIX_.'zhelpdesk_ticket_rating ORDER BY position'));		
	}	
	
	/**
	 * get all status to array
	 * @return array ('name'=>'name_status', 'value'=>'id_zhelpdesk_ticket_status')
	 */
	public static function getAllStatus()
	{
		global $cookie;
		
		return Db::getInstance()->ExecuteS(' 
			SELECT zts.id_zhelpdesk_ticket_status as value, ztsl.`status` as name 
			FROM '._DB_PREFIX_.'zhelpdesk_ticket_status zts 
			LEFT JOIN '._DB_PREFIX_.'zhelpdesk_ticket_status_lang ztsl ON (zts.id_zhelpdesk_ticket_status=ztsl.id_zhelpdesk_ticket_status AND id_lang='.$cookie->id_lang.')'
		);
	}
}

